/**
 *轻云蒜瓣商城账户面板
 */
Ext.define("PRO.shop.view.useraccount.accountPay",{
    extend: 'Ext.container.Container',
    layout:'column',
    alias: 'widget.shop.accountpay',
    autoScroll : true,
    callback:undefined,
    initComponent: function(){
        var me = this;
        //创建各项版块
        me.defaults = {columnWidth:1,border:0}
        me.items = 
            {
                xtype:'panel',
                margin:'10',
                itemId:'accountRecharge',
                bodyStyle:'padding:10px;width:1200px;',
                items:[
                    {
                        xtype:'dataview',
                        tpl: new Ext.XTemplate(
                            '<div class="je-dataview-tips">',
                                '<p class="je-dataview-tips-tit">温馨提示</p>',
                                '<ul>',
                                    '<li>1.线上充值金额会在当天到账。</li>',
                                    '<li>2.若选择“线下汇款”，提交线下打款凭证之前，请确保已准确对公打款至我司账户，账号不准确将导致充值失败。</li>',
                                    '<li>3.线下汇款（对公转账）一般两天到账，系统收到款项与打款凭证后会自动校对匹配并为您充值，请您耐心等待。如有疑问请联系客服400-0999-235。</li>',
                                '</ul>',
                            '</div>'
                        )
                    },
                    {
                        xtype: "box",
                        data:me.getData(),
                        itemId:'accountBlance',
                        tpl: new Ext.XTemplate(
                            '<div>',
                               '<p class="je-zmp-payTit">账户余额：</p>',
                               '<p class="je-zmp-payMoney">{charge}</p>',
                            '</div>'
                        )
                    },
                    {
                        xtype: "box",
                        data:{selectType:me.selectType!='aliPay'?true:false},
                        tpl: new Ext.XTemplate(
                            '<div>',
                               '<p class="je-zmp-payTit">充值方式：</p>',
                               '<div class="payPlanbox" style="margin-top:0;">',
                                    '<div class="payPlan payPlanNoboder">',
                                        '<div class="payPlanstyle" data-rechargeTypeCode="wechatPay">',
                                            '<i class="selectedIcon jeicon <tpl if="selectType">jeicon-check-circle<tpl else>jeicon-circle-o</tpl>"></i>',
                                            '<i class="payIcon jeicon jeicon-wechat"></i>',
                                            '<font>微信</font>',
                                        '</div>',
                                        '<div class="payPlanstyle payPlanpurse" data-rechargeTypeCode="aliPay">',
                                                '<i class="selectedIcon jeicon <tpl if="!selectType">jeicon-check-circle<tpl else>jeicon-circle-o</tpl>"></i>',
                                                '<i class="payIcon jeicon jeicon-alipay"></i>',
                                                '<font>支付宝</font>',
                                        '</div>',
                                        // '<div class="payPlanstyle">',
                                        //     '<i class="selectedIcon jeicon jeicon-circle-o"></i>',
                                        //     '<i class="payIcon jeicon jeicon-bankcard"></i>',
                                        //     '<font>线下付款</font>',
                                        // '</div>',
                                    '</div>',
                                '</div>',
                            '</div>'
                        ),
                        listeners:{
                            afterRender:function(){
                                var me = this;
                                me.getEl().on('click',function(e){
                                    if(e.getTarget('.payPlanstyle',3)){
                                        var payPlanstyle = e.getTarget('.payPlanstyle',3);
                                        var accountRecharge = me.up('[itemId=accountRecharge]');
                                        var payNow = accountRecharge.getCmp('reChargeInput').getCmp('payNow');
                                        payNow.selectType = payPlanstyle.getAttribute('data-rechargeTypeCode');
                                        var payBuy = payPlanstyle.querySelector('.selectedIcon');
                                        var otherPay= document.getElementsByClassName('selectedIcon');
                                        for(var i= 0 ;i<otherPay.length;i++){
                                            otherPay[i].setAttribute('class','selectedIcon jeicon jeicon-circle-o');
                                        }
                                        payBuy.setAttribute('class','selectedIcon jeicon jeicon-check-circle');
                                    }
                                })
                            }
                        }
                    },
                    {
                        xtype:'panel',
                        border:0,
                        itemId:'reChargeInput',
                        items:[
                            {
                                xtype: 'dataview',
                                tpl: new Ext.XTemplate(
                                   '<p style="margin-bottom:10px;"><span style="color:red;margin-right:10px;">*</span>充值金额</p>'
                                )
                            },
                            {
                                xtype: 'numberfield',
                                itemId:'rechargeMoney',
                                allowBlank: false,
                                emptyText:'0.01起',
                                labelStyle:'width:100%;display:block',
                                hideTrigger:true,//隐藏微调按钮,
                                enableKeyEvents:true,
                                listeners:{
                                    keyup:function(thisPanel,evObj,obj){
                                        var val = thisPanel.getValue();
                                        if(val>10000||val<0){
                                            JE.alert('单笔限额一万元，请重新输入！');
                                            thisPanel.setValue('')
                                        }
                                    }
                                }
                            },
                            {
                                xtype:'panel',
                                border:0,
                                hidden:true,
                                items:[
                                    {
                                        xtype: 'dataview',
                                        tpl: new Ext.XTemplate(
                                            '<div>',
                                                '<p class="je-pay-steps"><span>1</span>打款至蒜瓣对公收款账户</p>',
                                                '<p class="je-margin-left-20 je-pay-name je-zmp-payTit">收款户名</p>',
                                                '<p class="je-margin-left-20">北京凯特伟业科技有限公司</p>',
                                                '<p class="je-margin-left-20 je-zmp-payTit">收款账号</p>',
                                                '<p class="je-margin-left-20">11090 63088 10301</p>',
                                                '<p class="je-pay-steps"><span>2</span>在此提交线下打款凭证</p>',
                                            '</div>'
                                        )
                                    },
                                    {
                                        xtype: 'numberfield',
                                        fieldLabel:'汇款账户',
                                        allowBlank: false,
                                        labelStyle:'width:100%;display:block;font-size:16px;',
                                        hideTrigger:true//隐藏微调按钮
                                    },
                                    {
                                        xtype:'dataview',
                                        width:'400',
                                        height:'300',
                                        tpl:new Ext.XTemplate(
                                            '<div>',
                                                '<p class="je-zmp-payTit je-margin-left-20" style="margin-top:0;">打款凭证：</p>',
                                                '<p class="je-margin-left-20"></p>',
                                                '<p class="fe icon-doc-add je-zmp-addImg je-margin-left-20"></p>',
                                            '</div>'
                                        )
                                    }
                                ]
                                // }]
                            },
                            {
                                xtype:'button',
                                itemId:'payNow',
                                text:'立即支付',
                                selectType:me.selectType,
                                cls:'btn JEPLUS_B_L10',
                                style:'float:right;',
                                margin:'20 0 0 0',
                                handler:function(btn){
                                    var rechargeTypeCode = rechargeTypeCode;
                                    var accountRecharge = btn.up('[itemId=accountRecharge]').getCmp('accountRecharge');
                                    var rechargeMoney = btn.up('[itemId=accountRecharge]').getCmp('reChargeInput').getCmp('rechargeMoney').getValue();
                                    if(!JE.isEmpty(rechargeMoney)&&rechargeMoney!=0&&rechargeMoney>0&&rechargeMoney<=10000){
                                        JE.ajax({
                                            url:'/je/shoppingmall/bill/rechargeAccount',
                                            params:{
                                                amount:rechargeMoney,
                                                rechargeTypeCode:btn.selectType!='aliPay'?'2':'1'
                                            },
                                            success:function(res){
                                                var res = JSON.parse(res.responseText);
                                                if(res.success){
                                                    var rechargeTypeCode = btn.selectType!='aliPay'?'2':'1';
                                                    var bdrechargeNo = res.obj.rechargeNo;
                                                    switch(rechargeTypeCode){
                                                        case '1'://支付宝
                                                        var resHtml = res.obj.unifiedOrderRequestHtml;
                                                        btn.getEl().insertHtml('afterBegin',resHtml);
                                                        var form = document.querySelector('#alipaysubmit');
                                                            form.target = '_blank';
                                                            form.submit();
                                                        var shadowBox = btn.up();//btn.up('window').down('accountRecharge');
                                                        var myMask = new Ext.LoadMask(shadowBox, {msg:"正在支付，请稍后..."});
                                                        myMask.show();
                                                        window.PAY_SUCCESS_RES_PAGE = function(resData){
                                                            //本地充值账号和返回的账号编码应该一致
                                                            var context = JSON.parse(resData).context;
                                                            var resrechargeNo = JSON.parse(context).rechargeNo;
                                                            if(resrechargeNo==bdrechargeNo){
                                                                JE.win({
                                                                    width:290,
                                                                    height:330,
                                                                    border:0,
                                                                    bodyPadding:'20px,80px',
                                                                    default:{border:0},
                                                                    closable: false,
                                                                    maximizable :false,
                                                                    items:[{xtype:'shop.payhintview',info:{text:'支付宝支付成功',class:"jeicon-alipay"}}]
                                                                }).show();
                                                                var cardPanel = Ext.getCmp('shopMainEnterPanel').getCmp('cardPanel');
                                                                cardPanel.winType = 'recharge';
                                                                myMask.hide();
                                                                btn.up('window').close();
                                                                accountRecharge.upDate(me.getData())
                                                            }
                                                        }
                                                        break;
                                                        case '2'://微信
                                                        var qrCodePathSrc = "http://crm-private.oss-cn-beijing.aliyuncs.com"+res.obj.qrCodePath;
                                                        var money = res.obj.paymentAmount;
                                                        var payinfoview = JE.win({
                                                            width:350,
                                                            height:365,
                                                            border:0,
                                                            default:{border:0},
                                                            
                                                            items:[{xtype:'shop.payinfoview',info:{icon:'jeicon jeicon-wechatpay ',text:'微信扫码支付',money:money,img:'<img style="display:block;width:100%;" src='+qrCodePathSrc+'>'}}]
                                                        })
                                                        payinfoview.show();
                                                        window.PAY_SUCCESS_RES_PAGE = function(resData){
                                                            //本地充值账号和返回的账号编码应该一致
                                                            var context = JSON.parse(resData).context;
                                                            var resrechargeNo = JSON.parse(context).rechargeNo;
                                                            if(resrechargeNo==bdrechargeNo){
                                                                JE.win({
                                                                    width:290,
                                                                    height:330,
                                                                    border:0,
                                                                    bodyPadding:'20px,80px',
                                                                    default:{border:0},
                                                                    closable: false,
                                                                    maximizable :false,
                                                                    items:[{xtype:'shop.payhintview',info:{text:'微信支付成功',class:"jeicon-wechatpay"}}]
                                                                }).show();
                                                                var cardPanel = Ext.getCmp('shopMainEnterPanel').getCmp('cardPanel');
                                                                cardPanel.winType = 'recharge';
                                                                payinfoview.hide();
                                                                btn.up('window').close();
                                                                accountRecharge.upDate(me.getData())
                                                            }
                                                        }
                                                        break;
                                                    }
                                                }else{
                                                    JE.alert('充值失败，请联系管理员！')
                                                }
                                            }
                                        })
                                    }else{
                                        JE.alert('请输入正确的金额！')
                                        }
                                    }
                            }
                        ]
                    }
                ]
            }
        me.callParent(arguments);
    },
    getData:function(){
        var account = JE.ajax({
            url:'/je/shoppingmall/bill/getAccountInfo',
            params:''
        })
        if(account.success){
            var data = {
                charge:account.obj.values.ACCOUNT_BALANCE=='0'?'0.00':account.obj.values.ACCOUNT_BALANCE
            }
            return data
        }else{
            JE.alert(account.massage)
        }
    }
});